package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.CustomerMapper;
import com.gxa.zhonglong.pojo.Customer;
import com.gxa.zhonglong.service.CustomerService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther JJuventus
 * @date 2021/10/26 15:00
 */
@Service("CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper = null;

    @Override
    public List<Customer>  select(String want, Integer page, Integer size) {
        Integer offside = (page-1)*size;
        List<Customer>  customers = customerMapper.select(want, offside, size);
        return customers;
    }

    @Override
    public List<Customer> selectAll(String want) {
        return customerMapper.selectAll(want);
    }

    @Override
    public Integer add(Customer customer) throws BusinessException {
        Customer customer1 = customerMapper.selectByPhone(customer.getCustomerPhone());
        if (customer1==null){
            Integer add = customerMapper.add(customer);
            return add;
        }else{
            throw new BusinessException(ExceptionCode.DUPLICATE_CUSTOMER);
        }
    }

    @Override
    public Integer delete(Long customerId,String customerPhone) throws BusinessException {
        Customer customer1 = customerMapper.selectByPhone(customerPhone);
        if (customer1!=null){
            Integer delete = customerMapper.delete(customerId);
            return delete;
        }else{
            throw new BusinessException(ExceptionCode.NO_CUSTOMER);
        }
    }

    @Override
    public void update(Long customerId,String customerVipType, String customerPhone) {
        customerMapper.update(customerId, customerVipType, customerPhone);
    }
}
